<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <title>添加</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
        content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/layui/css/layui.css">

</head>

<body>
    <div style="padding: 20px;">
        <form class="layui-form layui-form-pane" action="" lay-filter="addForm">
            <div class=" layui-card-body  gridAuto">
                <div class="layui-form-item">
                    <label class="layui-form-label">菜单名称</label>
                    <div class="layui-input-block">
                        <input class="layui-input" lay-verify="required" name="menuname">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">路由</label>
                    <div class="layui-input-block">
                        <input class="layui-input" name="menulink">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">图标</label>
                    <div class="layui-input-block">
                        <input class="layui-input" id="iconPicker" lay-filter="iconPicker">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">状态</label>
                    <div class="layui-input-block">
                        <input type="checkbox" name="status" lay-skin="switch" lay-filter="switchTest" title="启用|禁用">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">排序</label>
                    <div class="layui-input-block">
                        <input type="number" lay-affix="number" min="0" name="order_num" value="0" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item" style="text-align: center; margin-top: 30px;">
                    <input type="hidden" name="id">
                    <input type="hidden" name="depth" id="depth">
                    <input type="hidden" name="icon" id="icon">
                    <button type="submit" class="layui-btn layui-btn-normal" lay-submit
                        lay-filter="YjbtnSave">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
    </div>
    <script src="/layui/layui.js"></script>
    <script>
        layui.config({
            base: '/src/'
        }).extend(
            { iconPicker: 'lib/extend/iconPicker' }
        ).use('iconPicker', function () {

            var form = layui.form
                , admin = parent.layui.admin
                , table = parent.layui.table
                , setter = layui.setter
                , iconPicker = layui.iconPicker
            $ = layui.$;


            // 通用获取URL参数方法
            function getQueryParam(name) {
                var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
                var r = window.location.search.substr(1).match(reg);
                if (r != null) return decodeURIComponent(r[2]);
                return null;
            }
            // 获取URL参数
            var pid = getQueryParam('pid') == '' ? 0 : getQueryParam('pid');
            var parent_path = getQueryParam('path');
            //初始化菜单选择器
            iconPicker.render({
                // 选择器，推荐使用input
                elem: '#iconPicker',
                // 数据类型：fontClass/unicode，推荐使用fontClass
                type: 'fontClass',
                // 是否开启搜索：true/false，默认true
                search: true,
                // 是否开启分页：true/false，默认true
                page: true,
                // 每页显示数量，默认12
                limit: 12,
                // 每个图标格子的宽度：'43px'或'20%'
                cellWidth: '43px',
                // 点击回调
                click: function (data) {
                    console.log(data.icon)
                    $("#icon").val(data.icon);
                }
            });
            initForm();

            // 初始化表单
            function initForm() {
                var id = getQueryParam('id');
                if (id) { // 编辑模式
                    admin.req({
                        url: 'sysmenu/GetMenuInfoByID/' + id,
                        done: function (res) {
                            if (res.successful) {
                                // 填充基础字段
                                form.val('addForm', {
                                    "id": res.data.id,
                                    "menuname": res.data.menuname,
                                    "menulink": res.data.menulink,
                                    "icon": res.data.icon,
                                    "status": res.data.status === 0,
                                    "depth": res.data.depth,
                                    "order_num":res.data.order_num
                                    

                                });
                                if (res.data.icon) {
                                    iconPicker.checkIcon('iconPicker', res.data.icon);
                                }

                            }
                        }
                    });
                }
            }
            form.on('submit(YjbtnSave)', function (data) {
                data.field.depth = parent_path.split(',').length - 1;
                var postData = {
                    ...data.field,
                    status: data.field.status ? 0 : 1,
                    pid: pid,
                    parent_path: parent_path,
                    icon: data.field.icon === '' ? "layui-icon-circle-dot" : data.field.icon //图标默认圆心
                };

                admin.req({
                    url: postData.id ? 'sysmenu/updateMenu' : 'sysmenu/addMenu',
                    method: postData.id ? 'PUT' : 'POST',
                    contentType: 'application/json',
                    data: JSON.stringify(postData),
                    done: function (res) {
                        if (res.successful) {
                            parent.layer.closeAll();
                            table.reload('LAY-app-menu-list');
                            admin.msg_success(postData.id ? '更新成功！' : '添加成功！');
                        }
                        else {
                            admin.msg_error(res.msg);
                        }
                    }
                });
            });

        })
    </script>
</body>

</html>